home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Gigarom 1
/
Gigarom Macintosh Archives (Quantum Leap)(CDRM1080320)(1993).iso
/
FILES
/
DEV
/
A-B
/
add images.cpt
/
pix_io.c
< prev
next >
Wrap
Text File
|
1989-06-22
|
10KB
|
418 lines
/*
pix_io.c
file input and output of PICTs and cut and paste with CLIPBOARD
and most important - conversion from std picts to the proper colors
for PROTON, FLOURINE, and the mixture of the two
*/
#include "pix_io.h"
#include "about_alert.h"
#include <IM1_5Protos.h>
extern short add_mode;
#define TRUE 1
#define FALSE 0
#define NIL 0
/* conversion to the proper pixmap could be tricky */
short short_max( short a, short b );
short short_min( short a, short b );
short short_max( a, b )
short a;
short b;
{
if ( a > b )
return( a );
else
return( b );
}
short short_min( a, b )
short a;
short b;
{
if ( a < b )
return( a );
else
return( b );
}
short GetPic( theReply, source_pic )
SFReply theReply;
PicHandle *source_pic;
/*
reads PICT file and stores in the
the pic handle. called by read_proton
and read_flourine.
*/
{
SFTypeList theList;
Point p;
short err, FileRef;
long BufSize;
PicHandle tempPic;
/* first, open PICT file */
FileRef = 0;
SetVol(NIL, theReply.vRefNum);
err = FSOpen( (Str255 *)&theReply.fName, theReply.vRefNum, &FileRef);
if ( err == noErr )
{
err = GetEOF(FileRef, &BufSize);
if (err == noErr)
{
/* size of the file includes a 512-byte header */
BufSize -= 512;
err = SetFPos(FileRef, fsFromStart, 512);
if ( err == noErr )
{
tempPic = (PicHandle)NewHandle(BufSize);
err = MemErr;
if ( err == noErr )
{
/* read the file (except the header) into the pic-handle */
HLock( (Handle)tempPic);
err = FSRead( FileRef, &BufSize, *tempPic );
HUnlock( (Handle)tempPic);
if ( err == noErr )
{
*source_pic = tempPic;
FileRef = FSClose(FileRef);
return( TRUE );
}
}
}
}
}
/* if we reach here, then we didn't successfully read in the image */
if (FileRef != 0)
{
FileRef = FSClose(FileRef);
}
D_error_alert( (long) err );
if (tempPic != NIL)
{
DisposHandle( (Handle) tempPic );
}
return( FALSE );
}
void PutPic( theReply, result_pic, thePalette )
/* write the Pixel image into a PICT file that uses thePalette */
SFReply theReply;
PicHandle result_pic;
PaletteHandle thePalette;
{
}
typedef unsigned char *BytePtr;
CGrafPtr pic_to_bmap( picH, frame, themaxdevice )
/* create off-screen pixmap and draw picture into it */
PicHandle picH;
Rect *frame;
GDHandle themaxdevice;
{
CGrafPtr off_port;
GDHandle olddevice;
GrafPtr saveport;
long size, offrowbytes;
Ptr image_space;
RgnHandle visregion;
short err, thedepth, i;
CTabHandle ourCMHandle;
/* check that the arguments makes sense */
if ( (*frame).top >= (*frame).bottom
|| (*frame).left >= (*frame).right
|| picH == NIL )
{
SysBeep(10);
return( NIL );
}
/* create first off-screen pixmap, using grayscale, to get images into 0-255 intensity scale */
off_port = (CGrafPtr) NewPtr( sizeof(CGrafPort) );
err = MemErr;
if ( err != noErr )
{
if ( off_port != NIL )
{
DisposPtr (off_port);
}
D_error_alert( (long) err );
return(NIL);
}
else
{
olddevice = GetGDevice();
GetPort(&saveport);
SetGDevice( themaxdevice );
OpenCPort(off_port);
thedepth = (**off_port->portPixMap).pixelSize;
offrowbytes = (((thedepth * (frame->right - frame->left)) + 15) >> 4) << 1;
size = (frame->bottom - frame->top) * (long) offrowbytes;
off_port->portPixMap = NewPixMap(); /* assume error-free ? */
image_space = NewPtr( size );
err = MemErr;
if ( err != noErr )
{
SetGDevice(olddevice);
SetPort(saveport);
if ( off_port != NIL )
{
DisposPtr (off_port);
}
if ( image_space != NIL )
{
DisposPtr (image_space);
}
D_error_alert( (long) err );
return(NIL);
}
else
{
(**(off_port->portPixMap)).rowBytes = offrowbytes + 0x8000;
(**(off_port->portPixMap)).baseAddr = image_space;
(**(off_port->portPixMap)).bounds = *frame;
ourCMHandle = (**(**themaxdevice).gdPMap).pmTable;
err = HandToHand( & ourCMHandle );
if ( err != noErr )
{
SetGDevice(olddevice);
SetPort(saveport);
if ( off_port != NIL )
{
DisposPtr (off_port);
}
if ( image_space != NIL )
{
DisposPtr (image_space);
}
D_error_alert( (long) err );
return(NIL);
}
else
{
for ( i = 0; i <= (**ourCMHandle).ctSize; ++i )
{
(**ourCMHandle).ctTable[i].value = i;
}
(**ourCMHandle).ctFlags &= 0x7fff;
(**ourCMHandle).ctSeed = GetCTSeed();
/* the above code is needed for converting gdevice cluts to pixmap cluts */
(**(*off_port).portPixMap).pmTable = ourCMHandle;
SetPort((GrafPtr) off_port);
RectRgn(off_port->visRgn, frame);
ClipRect( frame );
DrawPicture(picH, frame);
SetGDevice(olddevice);
SetPort(saveport);
return( off_port );
}
}
}
}
void add_bmaps( proton_bmap, proton_rect, pr_off_top, pr_off_left,
flourine_bmap, flourine_rect, fl_off_top, fl_off_left,
result_bmap, result_rect, themaxdevice )
/* create off-screen pixmap and the other two pixmaps into it */
CGrafPtr proton_bmap;
Rect *proton_rect;
CGrafPtr flourine_bmap;
Rect *flourine_rect;
CGrafPtr *result_bmap;
Rect *result_rect;
short pr_off_top, pr_off_left;
short fl_off_top, fl_off_left;
GDHandle themaxdevice;
{
CGrafPtr off_port;
GDHandle olddevice;
GrafPtr saveport;
long size, offrowbytes;
short r_top, r_left;
Ptr image_space;
Rect pr_off, fl_off;
RgnHandle visregion;
short err, thedepth, i;
CTabHandle ourCMHandle;
if ( pr_off_top < proton_rect->bottom /* test offsets for reasonableness */
&& pr_off_left < proton_rect->right
&& fl_off_top < flourine_rect->bottom
&& fl_off_left < flourine_rect->right
&& proton_rect->bottom > proton_rect->top /* test frames for sanity */
&& proton_rect->right > proton_rect->left
&& flourine_rect->bottom > flourine_rect->top
&& flourine_rect->right > flourine_rect->left
&& proton_rect->top == 0 /* these should always be 0 */
&& proton_rect->left == 0
&& flourine_rect->top == 0
&& flourine_rect->left == 0 )
{
result_rect->top = 0;
result_rect->left = 0;
pr_off = *proton_rect;
fl_off = *flourine_rect;
pr_off.top += pr_off_top;
pr_off.bottom += pr_off_top;
pr_off.left += pr_off_left;
pr_off.right += pr_off_left;
fl_off.top += fl_off_top;
fl_off.bottom += fl_off_top;
fl_off.right += fl_off_left;
fl_off.left += fl_off_left;
r_top = short_max( pr_off_top, fl_off_top );
r_left = short_max( pr_off_left, fl_off_left );
result_rect->right = r_left + short_max( proton_rect->right - pr_off_left,
flourine_rect->right - fl_off_left );
result_rect->bottom = r_top + short_max( proton_rect->bottom - pr_off_top,
flourine_rect->bottom - fl_off_top );
off_port = (CGrafPtr) NewPtr( sizeof(CGrafPort) );
err = MemErr;
if ( err != noErr )
{
if ( off_port != NIL )
{
DisposPtr (off_port);
}
D_error_alert( (long) err );
*result_bmap = NIL;
return;
}
else
{
olddevice = GetGDevice();
GetPort(&saveport);
SetGDevice( themaxdevice );
OpenCPort(off_port);
thedepth = (**off_port->portPixMap).pixelSize;
offrowbytes = (((thedepth * (result_rect->right - result_rect->left)) + 15) >> 4) << 1;
size = (result_rect->bottom - result_rect->top) * (long) offrowbytes;
off_port->portPixMap = NewPixMap(); /* assume error-free ? */
image_space = NewPtr( size );
err = MemErr;
if ( err != noErr )
{
SetGDevice(olddevice);
SetPort(saveport);
if ( off_port != NIL )
{
DisposPtr (off_port);
}
if ( image_space != NIL )
{
DisposPtr (image_space);
}
D_error_alert( (long) err );
*result_bmap = NIL;
return;
}
else
{
(**(off_port->portPixMap)).rowBytes = offrowbytes + 0x8000;
(**(off_port->portPixMap)).baseAddr = image_space;
(**(off_port->portPixMap)).bounds = *result_rect;
ourCMHandle = (**(**themaxdevice).gdPMap).pmTable;
err = HandToHand( & ourCMHandle );
if ( err != noErr )
{
SetGDevice(olddevice);
SetPort(saveport);
if ( off_port != NIL )
{
DisposPtr (off_port);
}
if ( image_space != NIL )
{
DisposPtr (image_space);
}
D_error_alert( (long) err );
*result_bmap = NIL;
return;
}
else
{
for ( i = 0; i <= (**ourCMHandle).ctSize; ++i )
{
(**ourCMHandle).ctTable[i].value = i;
}
(**ourCMHandle).ctFlags &= 0x7fff;
(**ourCMHandle).ctSeed = GetCTSeed();
/* the above code is needed for converting gdevice cluts to pixmap cluts */
(**(*off_port).portPixMap).pmTable = ourCMHandle;
SetPort((GrafPtr) off_port);
RectRgn(off_port->visRgn, result_rect);
ClipRect( result_rect );
/* copy over the bitmaps */
EraseRect( result_rect);
HLock((Handle) (off_port->portPixMap));
HLock((Handle) (proton_bmap->portPixMap));
CopyBits( (BitMap *) *(proton_bmap->portPixMap),
(BitMap *) *(off_port->portPixMap),
proton_rect, &pr_off, (short) srcCopy,
(RgnHandle) NIL);
HUnlock((Handle) (proton_bmap->portPixMap));
HLock((Handle) (flourine_bmap->portPixMap));
CopyBits( (BitMap *) *(flourine_bmap->portPixMap),
(BitMap *) *(off_port->portPixMap),
flourine_rect, &fl_off, (short) add_mode,
(RgnHandle) NIL);
HUnlock((Handle) (flourine_bmap->portPixMap));
HUnlock((Handle) (off_port->portPixMap));
SetGDevice(olddevice);
SetPort(saveport);
*result_bmap = off_port;
return;
}
}
}
}/* end if args reasonable */
}